class VotesController < ApplicationController

  before_filter :login_required

  def create
    if params[:user_id].blank? || params[:feature_request_id].blank? || !request.xhr?
      flash.now[:error] = 'Invalid vote submitted!'
      return
    end

    @vote = Vote.new(:user_id => params[:user_id].to_i,
                      :feature_request_id => params[:feature_request_id].to_i,
                      :ip_address => request.remote_ip.to_s)

    if @vote.save
      flash.now[:notice] = 'Vote successfully saved'
      @listing_all_frs = params[:listing_all_frs].to_i.zero? ? false : true
      @feature_requests = User.feature_requests(@listing_all_frs ? nil : @current_user.id)
    else
      flash.now[:error] = @vote.errors.full_messages.join('<br>')
      @feature_requests = nil
    end    
  end

end
